class QueriesController < ApplicationController
  def create
#    user = User.find(:first, :conditions => ["email = ? or mobile_no = ?", params[:user][:email].strip.downcase, params[:user][:mobile_no].strip.downcase])
#    if user.nil?
#      user = User.create(params[:user])
#      @verification_code= rand(9999999).to_s
#      user.verification_code  = @verification_code
#      if !params[:user][:email].empty?
#        user.name = params[:user][:email]
#      elsif !params[:user][:mobile_no].empty?
#        user.name = params[:user][:mobile_no]
#      end
#      user.save
#      @status="1"
#    else
#      @status="2"
#    end
#    advertisment        = Advertisment.find(params[:advertisment_id])
#    @advertisment       = advertisment
#    query               = Query.create(params[:query])
#    query.user          = user
#    query.advertisment  = advertisment
#    query.save
#    if params[:attachment]
#      attachment = Attachment.create
#      attachment.file = params[:attachment][:file]
#
#      attachment.query_id = query.id
#      attachment.save
#    end
#     session[:register_email] = params[:user][:email]
#      session[:user_type] = params[:user][:type]
#    redirect_to :controller => "advertisments", :action => "show", :id => advertisment, :user=>user.id,:s_status=>@status
#

    user = User.find(:first, :conditions => ["email = ? or mobile_no = ?", params[:user][:email].strip.downcase, params[:user][:mobile_no].strip.downcase])
    if user.nil?
            @status="1"
    else
            @status="2"
    end
    lead = Lead.create(params[:user])
    @verification_code= rand(9999999).to_s
    lead.verification_code  = @verification_code
    advertisment        = Advertisment.find(params[:advertisment_id])
    @advertisment       = advertisment
    query               = Query.create(params[:query])
    query.lead        = lead
    query.advertisment  = advertisment
    query.save
    if params[:attachment]
      attachment = Attachment.create
      attachment.file = params[:attachment][:file]

      attachment.query_id = query.id
      attachment.save
    end
     session[:register_email] = params[:user][:email]
      session[:user_type] = params[:user][:type]
    redirect_to :controller => "advertisments", :action => "show", :id => advertisment, :user=>lead.id,:s_status=>@status


 end
    def update
      @user=User.find_by_name(params[:user][:email])
      User.update(@user,:password=>params[:password][:original])

      redirect_to :controller => "advertisments", :action => "show", :id => params[:advertisment_id],:user_status=>@user
    end
end
